{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_X = np.random.random((10,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_X = raw_data_X *10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_y = np.array([0,0,0,0,0,1,1,1,1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = raw_data_X\n",
    "y_train = raw_data_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAADdVJREFUeJzt3UGIpHeZx/HfrzOzaMWldUkja2JX5bBkEYfdSB2iAQ9pFxbNmD3sIVARXRbqsmgUQZQ6SA61eBBpT0IR1xV8iYcxsDseRBldloUlUDMJ28mMIKzpdnSyaVm2FeuQkTx7eKuTmdnu9Nu9/dZbT9X3A0N1//NO9UPBfPP2W+/7liNCAIA8VpoeAABwPIQbAJIh3ACQDOEGgGQINwAkQ7gBIBnCDQDJEG4ASIZwA0AyZ+p40nvuuSc6nU4dTw0AC+ny5cu/joi1KtvWEu5Op6PxeFzHUwPAQrK9XXVbDpUAQDKEGwCSIdwAkAzhBoBkCDcAJEO4ASAZwg0AycxNuIutQp3NjlaeWlFns6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G4ASIZwA0AyhBsAkiHcAJAM4QaAZAg3ACRDuAEgmf8Fyb54/FyWOFoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1063a9c88>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0, 0], X_train[y_train==0,1], color='g')\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.random(2)*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8.67035706,  7.0760235 ])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAADgBJREFUeJzt3VGIrHd9xvHn2ewWnVhWSxapiTuTi5IiHtrIXEQDXmQtFM0xvehFYCJaCnNTNIogylxILqZ4IbJeCUOsFXyJF8dAe7wQZbWUQgnMnoRuco4g1Mx69KRZKV3FucjK+fXinUnOnu6efed033nf/8z3A4fZ/ec9sz8GzjfvvvO+7zgiBABIx0rVAwAAZkO4ASAxhBsAEkO4ASAxhBsAEkO4ASAxhBsAEkO4ASAxhBsAErNaxpPed9990Wq1ynhqAFhIu7u7v46IjSLblhLuVqul4XBYxlMDwEKyPSq6LYdKACAxhBsAEkO4ASAxhBsAEkO4ASAxhBs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W2/0PSn0v6+4rnmbvJbxyXJF2RtKe8KUtxFaXt5yT9u6SHbF+3/beSviLpL2z/TPlvI18p5Wdz5SQApIU9bgBIDOEGgMQQbgBIDOEGgMQQbgBIDOEGgMQQbgBIDOEGgMT8Lwi/oYhwODxFAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1063d8748>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0, 0], X_train[y_train==0,1], color='g')\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')\n",
    "plt.scatter(x[0], x[1], color='b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## kNN的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8.231110301142037,\n 2.467672103362692,\n 8.513701036889186,\n 7.405233631062757,\n 5.723981182780471,\n 4.7325594847194985,\n 3.026576262484981,\n 2.3280024672389885,\n 3.5998460221137596,\n 1.259337088954209]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from math import sqrt\n",
    "distances = []\n",
    "for x_train in X_train:\n",
    "    d = sqrt(np.sum((x-x_train)**2))\n",
    "    distances.append(d)\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 7, 1, 6, 8, 5, 4, 3, 0, 2])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_k = np.argsort(distances)\n",
    "top_k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设k取6\n",
    "k = 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 5)]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "votes = Counter(y_train[top_k[:k]])\n",
    "\n",
    "votes.most_common(1)    # 前边是标签，后边是个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最终kNN的结果：\n",
    "predict_y = votes.most_common(1)[0][0]\n",
    "predict_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run c1_knn/kNN.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y = kNN_classify(6, X_train, y_train, x)\n",
    "predict_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
